package com.androturka;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.androturka.IDataAdapter.EarthQuake;

class EarthQuakeAdapter extends BaseAdapter {

	private EarthQuake[] earthquakes;

	public EarthQuakeAdapter(Context context, EarthQuake[] earthquakes) {
		mContext = context;
		this.earthquakes = earthquakes;
	}

	@Override
	public int getCount() {
		return this.earthquakes.length;
	}

	@Override
	public Object getItem(int position) {
		return earthquakes[position];
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view;

		view = LayoutInflater.from(mContext).inflate(R.layout.earthquake_item,
				parent, false);
		TextView locationView = (TextView) view.findViewById(R.id.location);
		TextView magnitudeView = (TextView) view.findViewById(R.id.magnitude);
		TextView dateView = (TextView) view.findViewById(R.id.date);
		TextView distanceView = (TextView) view.findViewById(R.id.distance);

		locationView.setText(earthquakes[position].location);

		
		SimpleDateFormat df = new SimpleDateFormat("dd MMMM HH:mm");
		String formattedDate = df.format(earthquakes[position].date);

		dateView.setText(formattedDate + getTimeDifference(earthquakes[position].date) );
		
		//dateView.setText(formattedDate + " - " + dayDiff + " gün "+ hourDiff+" saat "+minuteDiff +" dakika önce");
		magnitudeView.setText(earthquakes[position].magnitude.toString());
		magnitudeView.setTextColor(getColor(earthquakes[position].magnitude));

		if ( earthquakes[position].distance != -1 )
		{
	        DecimalFormat dFormat = new DecimalFormat("#.##");
			distanceView.setText(dFormat.format(earthquakes[position].distance) + " km uzakta");
		}
		else
			distanceView.setText("Mesafeyi öğrenmek için GPS'inizi aktifleyin");

		return view;
	}

	private Context mContext;

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public static int getColor(double magnitude){
		
		if ( magnitude < 3.0 )
			return Color.rgb(0, 255, 0); //green
		else if ( magnitude < 5.0 )
			return Color.rgb(255, 255, 0); //yellow 
		else if ( magnitude < 6.0 )
			return Color.rgb(255, 128, 0); //orange
		else
			return Color.rgb(255, 0, 0); //red 			
	}
	
	public String getTimeDifference( Date date )
	{
		Date today = new Date();
		
		long diffInSec = ( today.getTime() - date.getTime())/1000;
		int dayDiff = (int) (diffInSec / ( 60 * 60 * 24));
		int hourDiff = (int)((diffInSec / ( 60 * 60)) % 24);
		int minuteDiff = (int) (diffInSec / 60) % 60;
		
		String diffStr = null;
		
		if ( ( dayDiff != 0 ) || ( hourDiff != 0 ) || ( minuteDiff != 0 ) )
		{
			diffStr = " - ";

			if ( dayDiff != 0 )
			{
				diffStr = diffStr.concat(dayDiff + " gün ");
				if ( hourDiff != 0 )
					diffStr = diffStr.concat(hourDiff + " saat ");				
			}
			else
			{	
				if ( hourDiff != 0 )
					diffStr = diffStr.concat(hourDiff + " saat ");				
				if ( minuteDiff != 0 )
					diffStr = diffStr.concat(minuteDiff + " dakika ");								
			}

			diffStr = diffStr.concat("önce");			
		}	
		
		return diffStr;
	}
	
}